package com.lzdongrui.client.controller;

import com.lzdongrui.backend.domain.Studentinfo;
import com.lzdongrui.backend.service.IStudentinfoService;
import com.lzdongrui.common.constant.CacheConstants;
import com.lzdongrui.common.constant.Constants;
import com.lzdongrui.common.core.domain.AjaxResult;
import com.lzdongrui.common.core.domain.entity.ClientMenu;
import com.lzdongrui.common.core.domain.entity.ClientUser;
import com.lzdongrui.common.core.domain.entity.SysMenu;
import com.lzdongrui.common.core.domain.entity.SysUser;
import com.lzdongrui.common.core.domain.model.LoginBody;
import com.lzdongrui.common.utils.SecurityUtils;
import com.lzdongrui.common.utils.StringUtils;
import com.lzdongrui.framework.web.service.ClientLoginService;
import com.lzdongrui.framework.web.service.ClientPermissionService;
import com.lzdongrui.framework.web.service.SysLoginService;
import com.lzdongrui.framework.web.service.SysPermissionService;
import com.lzdongrui.system.service.IClientUserMenuService;
import com.lzdongrui.system.service.ISysConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lzdongrui.backend.domain.dto.StudentinfoDTO;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/client")
public class ClientLoginController {

    @Autowired
    private ClientLoginService clientLoginService;

    @Autowired
    private ClientPermissionService clientPermissionService;

    @Autowired
    private IClientUserMenuService iClientUserMenuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private IStudentinfoService iStudentinfoService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();

        if (loginBody.getType()==1){
            // 生成令牌
            String token = clientLoginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                    loginBody.getUuid());
            ajax.put(Constants.TOKEN, token);
        }else{
            // 生成令牌
            String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                    loginBody.getUuid());
            ajax.put(Constants.TOKEN, token);
        }
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
//    @ClientLog(title = "获取个人信息", businessType = BusinessType.OTHER)
    public AjaxResult getInfo(@RequestParam("type") Integer type)
    {
        if (type == 1){
            ClientUser user = SecurityUtils.getClientLoginUser().getClientUser();
            SecurityUtils.getClientLoginUser().setClientUser(user);
            // 权限集合
            Set<String> permissions = clientPermissionService.getMenuPermission(user);
            AjaxResult ajax = AjaxResult.success();
            user.setPassword(null);
            ajax.put("user", user);
            ajax.put("roles", "commom");
            ajax.put("permissions", permissions);
            return ajax;
        } else {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            // 角色集合
            Set<String> roles = permissionService.getRolePermission(user);
            // 权限集合
            Set<String> permissions = permissionService.getMenuPermission(user);
            AjaxResult ajax = AjaxResult.success();
            user.setPassword(null);
            ajax.put("user", user);
            ajax.put("roles", roles);
            ajax.put("permissions", permissions);
            ajax.put("LoginPeople","Teacher");
            return ajax;
        }
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
//    @GetMapping("/getRouters")
//    public AjaxResult getRouters()
//    {
//        Long userId = SecurityUtils.getClientUserId();
//        List<SysMenu> menus = iClientUserMenuService.selectMenuTreeByUserId(userId);
//        return AjaxResult.success(iClientUserMenuService.buildMenus(menus));
//    }

}
